package com.joint.cloud.subject4_1_1.helloworld.controller;

import com.joint.cloud.common.constants.ResponseCode;
import com.joint.cloud.common.exception.BusinessException;
import com.joint.cloud.common.response.BaseResponse;
import com.joint.cloud.subject4_1_1.helloworld.constants.ApiConstant;
import com.joint.cloud.subject4_1_1.helloworld.controller.request.DemoParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(ApiConstant.VERSION + "/demo")
@Slf4j
@Api(tags = {"示例"})
@BaseResponse
public class DemoController {

    @ApiOperation("URL入参示例")
    @GetMapping("/param/1/{id}")
    @ResponseBody
    public Long paramTest1(@ApiParam("测试入参id") @PathVariable("id") Long id) {
        log.info("接到入参: {}", id);
        log.warn("接到入参: {}", id);
        log.debug("接到入参: {}", id);
        log.error("接到入参: {}", id);
        return id;
    }

    @ApiOperation("param入参示例")
    @GetMapping("/param/2")
    public Object paramTest2(@ApiParam("测试入参key") @RequestParam("key") Long key) {
        return key;
    }

    @ApiOperation("body入参示例")
    @PostMapping("/param/3")
    public DemoParam paramTest3(@Validated @ApiParam("测试入参demoParam") @RequestBody DemoParam demoParam) {
        return demoParam;
    }

    @ApiOperation("统一返回、统一异常示例")
    @GetMapping("/{userId}")
    public Object getUserById(@PathVariable Long userId) {
        if (userId.equals(0L)) {
            throw new BusinessException(ResponseCode.BAD_REQUEST, "提示信息");
        } else if (userId.equals(1L)) {
            int i = 1 / 0;
        }
        return userId;
    }
}

